<template>
  <div :class="'text-message ' + (isMine ? 'my-text' : '')">
    <div v-for="(item, index) in renderDom" :key="index" class="message-body-span">
      <span class="message-body-span-text" v-if="item.name === 'span'">{{ item.text }}</span>
      <img v-if="item.name === 'img'" class="emoji-icon" :src="item.src" alt="">
    </div>
  </div>
</template>

<script>
import {parseText} from "@/utils/im";

export default {
  name: "MessageText",
  data() {
    return {
      renderDom: []
    };
  },
  components: {},
  props: {
    message: {
      type: Object
    },
    isMine: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    message: {
      handler: function(newVal) {
        this.renderDom=parseText(newVal);
      },
      immediate: true,
      deep: true
    }
  },

  beforeMount() {
    // 在组件实例进入页面节点树时执行
  },

  destroyed() {
    // 在组件实例被从页面节点树移除时执行
  },

  methods: {}
}
</script>

<style lang="scss" scoped>
.text-message {
  display: inline-flex;
  max-width: 60vw;
  line-height: 26px;
  padding: 6px 12px;
  background: #FFF;
  /* border: 1px solid #D9D9D9; */
  box-shadow: 0 0 3px 0 #ccc;
  border-radius: 2px 10px 10px 10px;
}

.my-text {
  border-radius: 10px 2px 10px 10px;
  border: 1px solid rgba(0, 110, 255, 0.30);
  background: rgba(0, 110, 255, 0.10);
}

.message-body-span {
  display: flex;
  justify-content: center;
  align-items: center;
  /*justify-content: flex-start;*/
  flex-wrap: wrap;
  outline: none;
  font-size: 14px;
  color: #333333;
  position: relative;
  max-width: 60vw;
}

.message-body-span-text {
  width: 100%;
  display: inline;
  word-wrap: break-word;
  word-break: break-all;
}

.message-body-span-image {
  display: inline-block;
  width: 16px;
  height: 16px;
  margin: 0 2px;
}

.emoji-icon {
  width: 20px;
  height: 20px;
}

</style>
